<?php
/*
Vega_Session_Memcache - memcache session handler
source: http://vn2.php.net/manual/en/function.session-set-save-handler.php#83096
*/
class Vega_Session_Memcache
{
    private static $lifetime = 0;

    public static function open()
    {
        self::$lifetime = ini_get('session.gc_maxlifetime');
        
        return true;
    }
    
    public static function read($id)
    {
        return Memcached::get("sessions/{$id}");
    }
    
    public static function write($id, $data)
    {
        return Memcached::set("sessions/{$id}", $data, self::$lifetime);
    }
    
    public static function destroy($id)
    {
        return Memcached::delete("sessions/{$id}");
    }
    
    public function __construct(){
        // register the new handler
        session_set_save_handler(
            array(&$this, 'open'),
            array(&$this, 'close'),
            array(&$this, 'read'),
            array(&$this, 'write'),
            array(&$this, 'destroy'),
            array(&$this, 'gc')
        );
		register_shutdown_function('session_write_close');
		session_start();
	}
    public static function gc(){ return true; }
    public static function close(){    return true; }
    public function __destruct()
    {
        session_write_close();
    }
}